package com.springboot.cloud.demos.ribbon.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.exception.SystemErrorType;
import com.springboot.cloud.common.web.entity.constant.WeixinConstant;
import com.springboot.cloud.demos.entity.form.UserForm;
import com.springboot.cloud.demos.entity.form.UserQueryForm;
import com.springboot.cloud.demos.entity.form.UserRequestEntity;
import com.springboot.cloud.demos.entity.param.CouponQueryParam;
import com.springboot.cloud.demos.entity.param.FollowQueryParam;
import com.springboot.cloud.demos.entity.po.User;
import com.springboot.cloud.demos.ribbon.weixin.AES;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.AlgorithmParameters;
import java.security.Security;
import java.util.Arrays;

@Slf4j
@Service
public class UserService {

    @Autowired
    RestTemplate restTemplate;

    @SentinelResource(fallback = "updateFallback")
    public Result update(@RequestBody User user) {
        restTemplate.put("http://producer/user/{1}", user, user.getId());
        return Result.success();
    }

    @SentinelResource(fallback = "getFallback")
    public Result<User> queryByMobile(String username) {
        return restTemplate.getForEntity("http://producer/user/queryByMobile?mobile={1}", Result.class, username).getBody();

    }

    @SentinelResource(fallback = "getFallback")
    public Result getStatistics(String id) {
        return restTemplate.getForEntity("http://producer/user/getStatistics?userId={1}", Result.class, id).getBody();
    }

    @SentinelResource(fallback = "conditionsFallback")
    public Result<Page> nearBy(@RequestBody UserQueryForm param) {
        return restTemplate.postForEntity("http://producer/user/nearBy", param, Result.class).getBody();
    }

    @SentinelResource(fallback = "conditionsFallback")
    public Result<Page> recommend(@RequestBody UserQueryForm param) {
        return restTemplate.postForEntity("http://producer/user/recommend", param, Result.class).getBody();
    }

    @SentinelResource(fallback = "conditionsFallback")
    public Result<Page> getFollowMe(@RequestBody UserQueryForm param) {
        return restTemplate.postForEntity("http://producer/user/getFollowMe", param, Result.class).getBody();
    }

    @SentinelResource(fallback = "conditionsFallback")
    public Result<Page> getMyFollow(@RequestBody UserQueryForm param) {
        return restTemplate.postForEntity("http://producer/user/getMyFollow", param, Result.class).getBody();
    }

    public Result wxLoginFallback(@RequestBody UserRequestEntity userRequestEntity) {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }

    public Result updateFallback(@RequestBody User userRequestEntity) {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }

    public Result getUserInfoFallback(@RequestBody UserRequestEntity userRequestEntity) {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }

    public Result getFallback(@RequestParam String id) {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }

    public Result addFallback(@RequestBody UserForm userForm) {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }

    @SentinelResource(fallback = "addFallback")
    public Result add(@RequestBody UserForm userForm) {
        Result<User> result = restTemplate.postForEntity("http://producer/user", userForm, Result.class).getBody();

        return result;
    }

    @SentinelResource(fallback = "getFallback")
    public Result<User> getById(String id) {
        return restTemplate.getForEntity("http://producer/user/{1}", Result.class, id).getBody();
    }
}
